import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

/***
 * Dio Example: https://github.com/cfug/dio/tree/main/example
 * Dio 文档： https://pub.dev/packages/dio/example
 */
class DioView extends StatefulWidget {
  const DioView({super.key});

  @override
  State<DioView> createState() => _DioViewState();
}

class _DioViewState extends State<DioView> {
  _getData() async {
    var response = await Dio().get('https://jdmall.itying.com/api/httpGet',
        queryParameters: {"username": "汪方晨", "age": 33});
    print('_getData 请求:${response.data is Map}');
    print('_getData 返回数据 data:: ${response.data}');
    print('_getData 返回数据: ${response.data["msg"]}');
  }

  _postData() async {
    var response = await Dio().post('https://jdmall.itying.com/api/httpPost',
        data: {"username": "汪方晨", "age": "33"});

    print('_postData 请求:${response.data}');
    print('_postData 返回数据: ${response.data is Map}');
  }

  _putData() async {
    var response = await Dio().put('https://jdmall.itying.com/api/httpPut',
        data: {"username": "汪方晨", "age": "32", 'sex': '男'});

    print('_putData 请求返回数据:${response.data}');
    print('_putData 返回数据: ${response.data is Map}');
  }

  _deleteData() async {
    var response = await Dio().delete(
        'https://jdmall.itying.com/api/httpDelete',
        queryParameters: {"id": "1"});

    print('_deleteData 请求返回数据:${response.data}');
    print('_deleteData 返回数据: ${response.data is Map}');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Dio get post put del 请求测试'),
        ),
        body: Container(
            width: double.maxFinite,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colors.blue),
                      foregroundColor: MaterialStateProperty.all(Colors.white),
                      elevation: MaterialStateProperty.all(20),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10))),
                    ),
                    onPressed: () {
                      print("Get onPressed ");
                      _getData();
                    },
                    child: const Text('Get 请求测试')),
                ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colors.blue),
                      foregroundColor: MaterialStateProperty.all(Colors.white),
                      elevation: MaterialStateProperty.all(20),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10))),
                    ),
                    onPressed: () {
                      print("Post onPressed ");
                      _postData();
                    },
                    child: const Text('Post 请求测试')),
                ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colors.blue),
                      foregroundColor: MaterialStateProperty.all(Colors.white),
                      elevation: MaterialStateProperty.all(20),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10))),
                    ),
                    onPressed: () {
                      print("Put onPressed ");
                      _putData();
                    },
                    child: const Text('Put 请求测试')),
                ElevatedButton(
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.all(Colors.blue),
                      foregroundColor: MaterialStateProperty.all(Colors.white),
                      elevation: MaterialStateProperty.all(20),
                      shape: MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10))),
                    ),
                    onPressed: () {
                      print("Del onPressed ");
                      _deleteData();
                    },
                    child: const Text('Del 请求测试')),
              ],
            )));
  }
}
